//! 错误处理模块
//! 
//! 定义Yaya库的错误类型和处理机制

use thiserror::Error;

/// Yaya库的错误类型
#[derive(Error, Debug, Clone)]
pub enum YayaError {
    /// 内存不足错误
    #[error("内存不足")]
    OutOfMemory,
    
    /// 无效参数错误
    #[error("无效参数: {0}")]
    InvalidParameter(String),
    
    /// 硬件错误
    #[error("硬件错误: {0}")]
    HardwareError(String),
    
    /// 文件系统错误
    #[error("文件系统错误: {0}")]
    FileSystemError(String),
    
    /// 网络错误
    #[error("网络错误: {0}")]
    NetworkError(String),
    
    /// 缓冲区太小错误
    #[error("缓冲区太小")]
    BufferTooSmall,
    
    /// 操作不支持错误
    #[error("操作不支持: {0}")]
    UnsupportedOperation(String),
    
    /// 超时错误
    #[error("操作超时")]
    Timeout,
    
    /// 内部错误
    #[error("内部错误: {0}")]
    InternalError(String),
    
    /// 未知错误
    #[error("未知错误")]
    Unknown,
}

/// Yaya库的结果类型别名
pub type Result<T> = core::result::Result<T, YayaError>;

impl YayaError {
    /// 检查错误是否可恢复
    pub fn is_recoverable(&self) -> bool {
        match self {
            YayaError::Timeout | YayaError::BufferTooSmall => true,
            _ => false,
        }
    }
    
    /// 获取错误描述
    pub fn description(&self) -> &str {
        match self {
            YayaError::OutOfMemory => "内存不足",
            YayaError::InvalidParameter(_) => "无效参数",
            YayaError::HardwareError(_) => "硬件错误",
            YayaError::FileSystemError(_) => "文件系统错误",
            YayaError::NetworkError(_) => "网络错误",
            YayaError::BufferTooSmall => "缓冲区太小",
            YayaError::UnsupportedOperation(_) => "操作不支持",
            YayaError::Timeout => "操作超时",
            YayaError::InternalError(_) => "内部错误",
            YayaError::Unknown => "未知错误",
        }
    }
}